package com.icen.utils.shared;

import java.util.Map;
import java.util.Map.Entry;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * SharedPreferences Utils Class
 * 
 * @author IcenHan 2015-01-15
 */
public class SharedPreUtils {
	private String mFileName = "iSharedPreUtils";

	private static SharedPreUtils mSharedPreUtils = null;
	private static SharedPreferences mSharedPre = null;

	private SharedPreUtils() {
	}

	private void initSharedPre(Context context, String fileName) {
		if (fileName != null) {
			this.mFileName = fileName;
		}
		mSharedPre = context.getSharedPreferences(mFileName,
				Context.MODE_PRIVATE);
	}

	/**
	 * 获取工具类实例
	 * 
	 * @param context
	 * @return SharedPreUtils
	 */
	public static SharedPreUtils getInstance(Context context) {
		if (mSharedPreUtils == null) {
			mSharedPreUtils = new SharedPreUtils();
		}
		mSharedPreUtils.initSharedPre(context, null);
		return mSharedPreUtils;
	}

	/**
	 * 获取工具类实例
	 * 
	 * @param context
	 * @param fileName
	 *            文件名称
	 * @return SharedPreUtils
	 */
	public static SharedPreUtils getInstance(Context context, String fileName) {
		if (mSharedPreUtils == null) {
			mSharedPreUtils = new SharedPreUtils();
		}
		mSharedPreUtils.initSharedPre(context, fileName);
		return mSharedPreUtils;
	}

	/**
	 * 读取文件中值:String
	 * 
	 * @param key
	 * @param defultValue
	 * @return String
	 */
	public String getString(String key, String defultValue) {
		return mSharedPre.getString(key, defultValue);
	}

	/**
	 * 读取文件中值:boolean
	 * 
	 * @param key
	 * @param defultValue
	 * @return boolean
	 */
	public boolean getBoolean(String key, Boolean defultValue) {
		return mSharedPre.getBoolean(key, defultValue);
	}

	/**
	 * 读取文件中值:int
	 * 
	 * @param key
	 * @param defultValue
	 * @return int
	 */
	public int getInteger(String key, int defultValue) {
		return mSharedPre.getInt(key, defultValue);
	}

	/**
	 * 读取文件中值:long
	 * 
	 * @param key
	 * @param defultValue
	 * @return long
	 */
	public long getLong(String key, long defultValue) {
		return mSharedPre.getLong(key, defultValue);
	}

	/**
	 * 读取文件中值:float
	 * 
	 * @param key
	 * @param defultValue
	 * @return float
	 */
	public float getFloat(String key, float defultValue) {
		return mSharedPre.getFloat(key, defultValue);
	}

	/**
	 * 读取文件中的所有值
	 * 
	 * @return Map<String, Object>
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> getAll() {
		return (Map<String, Object>) mSharedPre.getAll();
	}

	/**
	 * 保存Map集合
	 * 
	 * @param sharedPreMap
	 *            需要保存的Map集合
	 */
	public void setValue(Map<String, Object> sharedPreMap) {
		if (sharedPreMap != null) {
			for (Entry<String, Object> entry : sharedPreMap.entrySet()) {
				this.setValue(entry.getKey(), entry.getValue());
			}
		}
	}

	/**
	 * 保存键值对
	 * 
	 * @param key
	 * @param value
	 * @return boolean
	 */
	public boolean setValue(String key, Object value) {
		Editor edit = mSharedPre.edit();
		if (value instanceof Boolean) {
			edit.putBoolean(key, (Boolean) value);
		} else if (value instanceof Integer || value instanceof Byte) {
			edit.putInt(key, (Integer) value);
		} else if (value instanceof Long) {
			edit.putLong(key, (Long) value);
		} else if (value instanceof Float) {
			edit.putFloat(key, (Float) value);
		} else if (value instanceof String) {
			edit.putString(key, (String) value);
		} else {
			edit.putString(key, value.toString());
		}
		return edit.commit();
	}

	/**
	 * 移除文件中的某个值
	 * 
	 * @param key
	 * @return boolean
	 */
	public boolean removeValue(String key) {
		return mSharedPre.edit().remove(key).commit();
	}

	/**
	 * 清空文件中的所有键值对
	 * 
	 * @return boolean
	 */
	public boolean clearValue() {
		return mSharedPre.edit().clear().commit();
	}
}
